<template>
  <div class="manager-container">
    <!-- 头部 -->
    <div class="manager-header">
      <div class="manager-header-left">
        <img src="@/assets/imgs/logo.png" />
        <div class="title">BC-FL</div>
      </div>

      <div class="manager-header-center">
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
          <el-breadcrumb-item :to="{ path: $route.path }">{{ $route.meta.name }}</el-breadcrumb-item>
        </el-breadcrumb>
      </div>

      <div class="manager-header-right">
        <el-dropdown placement="bottom">
          <div class="avatar">
            <img :src="user.avatar || 'https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png'" />
            <div>{{ user.name || '管理员' }}</div>
          </div>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item @click.native="goToPerson">个人信息</el-dropdown-item>
            <el-dropdown-item @click.native="$router.push('/password')">修改密码</el-dropdown-item>
            <el-dropdown-item @click.native="logout">退出登录</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </div>
    </div>

    <!-- 主体 -->
    <div class="manager-main">
      <!-- 侧边栏 -->
      <div class="manager-main-left">
      <el-menu 
        :default-active="$route.path"
        :openeds="openedSubmenus"
        router
        style="border: none"
        @open="handleOpen"
        @close="handleClose"
      >
          <!-- <el-menu-item index="/home">
            <i class="el-icon-s-home"></i>
            <span slot="title">系统首页</span>
          </el-menu-item> -->

          <el-submenu index="fed">
            <template slot="title">
              <i class="el-icon-menu"></i><span>联邦学习任务管理</span>
            </template>
            <el-menu-item index="/flTaskManagement">任务发布信息</el-menu-item>
            <!-- <el-menu-item index="/taskProgress">任务处理信息</el-menu-item> -->
            <!-- <el-menu-item index="/client">成员列表</el-menu-item> -->
          </el-submenu>
          <el-submenu index="fed">
            <template slot="title">
              <i class="el-icon-s-home"></i><span>系统成员</span>
            </template>
            <el-menu-item index="/client">成员列表</el-menu-item>
          </el-submenu>
          <el-submenu index="block">
            <template slot="title">
              <i class="el-icon-connection"></i> <span>区块链管理</span>
            </template>
            <el-menu-item index="/blockchainManagement">区块链信息</el-menu-item>
          </el-submenu>

          <!-- <el-submenu index="ipfs">
            <template slot="title">
              <i class="el-icon-copy-document"></i><span>IPFS信息管理</span>
            </template>
            <el-menu-item index="/ipfs">IPFS信息</el-menu-item>
          </el-submenu> -->

          <!-- <el-submenu index="trace">
            <template slot="title">
              <i class=" el-icon-notebook-2 "></i><span>任务结果追溯管理</span>
            </template>
            <el-menu-item index="/taskTraceability">任务结果追溯</el-menu-item>
          </el-submenu> -->

        </el-menu>
      </div>

      <!-- 数据表格 -->
      <div class="manager-main-right">
        <router-view @update:user="updateUser" />
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "Manager",
  data() {
    return {
      user: JSON.parse(localStorage.getItem('xm-user') || '{}'),  
          openedSubmenus: [] // 改为动态控制展开状态

    }
  },
  created() {
    if (!this.user.id) {
      this.$router.push('/login')
    }
  },
  watch: {
    '$route.path': {
      immediate: true,
      handler(newPath) {
        // 根据当前路径自动展开对应子菜单
        const menuMap = {
          '/flTaskManagement': 'fed',
          '/blockchainManagement': 'block',
          '/ipfs': 'ipfs',
          '/taskTraceability': 'trace',
          '/client': 'fed',
          '/taskProgress': 'fed',
        }
        const currentMenu = menuMap[newPath]
        if (currentMenu && !this.openedSubmenus.includes(currentMenu)) {
          this.openedSubmenus = [currentMenu]
        }
      }
    }
  },
  methods: {
    updateUser() {
      this.user = JSON.parse(localStorage.getItem('xm-user') || '{}') // 重新获取下用户的最新信息
    },
    goToPerson() {
      if (this.user.role === 'ADMIN') {
        this.$router.push('/adminPerson')
      }
    },
    logout() {
      localStorage.removeItem('xm-user')
      this.$router.push('/login')
    },  // 处理子菜单展开
    handleOpen(index) {
      if (!this.openedSubmenus.includes(index)) {
        this.openedSubmenus = [...this.openedSubmenus, index]
      }
    },
    // 处理子菜单关闭
    handleClose(index) {
      this.openedSubmenus = this.openedSubmenus.filter(i => i !== index)
    }
  }
}
</script>

<style scoped>
@import "@/assets/css/manager.css";
</style>
